package com.xc.ezplan.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.xc.ezplan.admin.entity.AdminMenu;
import com.xc.ezplan.admin.service.AdminMenuService;
import com.xc.ezplan.admin.service.AdminPurviewService;
import com.xc.ezplan.admin.vo.AdminMenuVo;
import com.xc.ezplan.core.base.BaseSearchBody;
import com.xc.ezplan.core.base.EzResult;
import com.xc.ezplan.core.exception.BadRequestException;
import com.xc.ezplan.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lixingchen
 * @date 2022/11/19
 * @description 菜单管理
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单管理")
@RequiredArgsConstructor
public class AdminMenuController {

    private final AdminMenuService adminMenuService;

    private final AdminPurviewService adminPurviewService;

    private static final String ENTITY_NAME = "menu";

    @Log("新增菜单")
    @ApiOperation("新增菜单")
    @PostMapping
    @SaCheckPermission("menu.add")
    public EzResult<Object> createMenu(@Validated @RequestBody AdminMenuVo adminMenuVo){
        if (adminMenuVo.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        adminMenuService.createMenu(adminMenuVo);
        return EzResult.ok();
    }

    @Log("删除菜单")
    @ApiOperation("删除菜单")
    @DeleteMapping
    @SaCheckPermission("menu.delete")
    public EzResult<Object> deleteMenu(@RequestBody List<Long> ids){
        adminMenuService.deleteMenu(ids);
        return EzResult.ok();
    }

    @Log("修改菜单")
    @ApiOperation("修改菜单")
    @PutMapping
    @SaCheckPermission("menu.update")
    public EzResult<Object> updateMenu(@Validated @RequestBody AdminMenuVo adminMenuVo){
        adminMenuService.updateMenu(adminMenuVo);
        return EzResult.ok();
    }

    @GetMapping
    @ApiOperation("查询菜单")
    public EzResult<Object> queryMenu(@RequestParam(required = false) Long id){
        return EzResult.ok(adminMenuService.queryMenu(id));
    }

    @ApiOperation("按条件分页查询菜单")
    @PostMapping("/page")
    public EzResult<Object> pageQueryMenu(@RequestBody BaseSearchBody<AdminMenu> baseSearchBody) {
        return EzResult.ok(adminMenuService.pageQueryMenu(baseSearchBody.getSearchCondition(), baseSearchBody.getPage()));
    }

    @Log("使菜单隐藏/显示")
    @ApiOperation("使菜单隐藏/显示")
    @PutMapping("/changeHidden")
    @SaCheckPermission("menu.hide")
    public EzResult<Object> changeHidden(@RequestParam Long id,@RequestParam Boolean hidden) {
        adminMenuService.changeHidden(id,hidden);
        return EzResult.ok();
    }

    @GetMapping(value = "/build")
    @ApiOperation("获取前端所需菜单")
    public EzResult<Object> buildMenus(){
        return EzResult.ok(adminMenuService.buildMenus());
    }

    @ApiOperation("返回全部的菜单")
    @GetMapping(value = "/lazy")
    public EzResult<Object> queryAllMenu(@RequestParam Long pid){
        return EzResult.ok(adminMenuService.queryAllMenu(pid));
    }

    @ApiOperation("根据菜单ID返回所有子节点ID，包含自身ID")
    @GetMapping(value = "/child")
    public EzResult<Object> childMenu(@RequestParam Long id){
        return EzResult.ok(adminMenuService.childMenu(id));
    }

    @ApiOperation("查询菜单:根据ID获取同级与上级数据")
    @PostMapping("/superior")
    public EzResult<Object> getMenuSuperior(@RequestBody List<Long> ids) {
        return EzResult.ok();
    }

    @ApiOperation("导出菜单数据")
    @GetMapping(value = "/download")
    public void exportMenu(HttpServletResponse response, AdminMenuVo adminMenuVo) throws Exception {
    }

    @ApiOperation("获取用户权限")
    @GetMapping(value = "/permissions")
    public EzResult<Object> getPermissionsByUser() {
        long userId = StpUtil.getLoginIdAsLong();
        return EzResult.ok(adminPurviewService.getPermissionsByUser(userId));
    }

	@ApiOperation("获取用户角色")
	@GetMapping(value = "/roles")
	public EzResult<Object> getRolesByUser() {
        long userId = StpUtil.getLoginIdAsLong();
		return EzResult.ok(adminPurviewService.getRolesByUser(userId));
	}

}
